package org.example;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * ClassName: WuDaInterceptor
 * Package: org.example
 * Description:
 *
 * @Author wanghao
 * @Create 2024-07-08 9:13
 * @Version 1.0
 */
public class WuDaInterceptor implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        String methodName = method.getName();
        if (methodName.endsWith("Bing")) {
            song();
        }
        return methodProxy.invokeSuper(o, objects);
    }

    private void song() {
        System.out.println("送大麦茶");
    }

    public Object createProxy(Object target) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }
}
